package com.imooc.exception;

import com.imooc.utils.JSONResult;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 统一异常拦截处理
 * 可以针对异常自定义去处理去捕获，返回指定的类型（json类型）到前端
 */
@ControllerAdvice
public class GraceExceptionHandler {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public JSONResult returnMaxFileSizeLimit() {
        return JSONResult.errorMsg("文件大小不能超过500KB");
    }

    // 对自定义异常统一进行拦截
    @ExceptionHandler(MyCustomException.class)
    @ResponseBody
    public JSONResult returnMyCustomException(MyCustomException e) {
        return JSONResult.errorMsg(e.getMessage());
    }
}
